//
//  MCSettingMoneyViewController.swift
//  StoreManage
//
//  Created by MC on 2018/5/18.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

protocol MCSettingMoneyViewControllerDelegate : NSObjectProtocol {
    func MCSettingMoneyViewController_SettingMoney(moneyStr:String)
}

class MCSettingMoneyViewController: MCViewController {

    weak var delegate : MCSettingMoneyViewControllerDelegate?
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        textField.becomeFirstResponder()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func saveButtonClicked() {
        
        if (textField.text?.isEmpty)! {
            MCHUD_text(message: "请输入金额", view: self.view, delay: 1.2)
            return
        }
        
        delegate?.MCSettingMoneyViewController_SettingMoney(moneyStr: textField.text!)
        navigationController?.popViewController(animated: true)
    }

    // MARK: - Network
    func sendNetworking() {
        
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "设置金额"
        self.view.backgroundColor = UIColor.white
    }
    
    func initUI() {
        view.addSubview(textField)
        textField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-45)
            make.top.equalTo(view).offset(40)
            make.height.equalTo(40)
        }
        
        view.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.bottom.equalTo(textField)
            make.right.equalTo(view).offset(-15)
            make.height.equalTo(1)
        }
        
        view.addSubview(saveButton)
        saveButton.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize.init(width: MCScreenWidth - 80, height: 40))
            make.top.equalTo(lineView.snp.bottom).offset(36)
        }
    }
    
    // MARK: - Setter & Getter
    lazy var textField: MCTextFiled = {
        let tf = MCTextFiled()
        tf.textAlignment = NSTextAlignment.center
        tf.keyboardType = UIKeyboardType.decimalPad
        tf.placeholder = "请输入金额"
        
        let label = UILabel()
        label.font = MCFont(14)
        label.text = "¥"
        label.textColor = UIColor.MCWords
        label.textAlignment = NSTextAlignment.right
        label.frame = CGRect.init(x: 0, y: 0, width: 30, height: 10)
        tf.leftViewMode = UITextFieldViewMode.always
        tf.leftView = label
        return tf
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_middle
        return view
    }()
    
    
    lazy var saveButton: UIButton = {
        let button = UIButton.init(type: .custom)
        button.titleLabel?.font = MCFont(14)
        button.backgroundColor = UIColor.MCMain
        button.setTitle("确定", for: .normal)
        button.layer.cornerRadius = 5
        button.layer.masksToBounds = true
        button.setTitleColor(UIColor.white, for: .normal)
        button.addTarget(self, action: #selector(saveButtonClicked), for: .touchUpInside)
        return button
    }()

}
